//
//  ConfigureBaseCell.m
//  JinganMindCloud
//
//  Created by JinganMac on 2020/6/29.
//  Copyright © 2020 JingAn. All rights reserved.
//

#import "ConfigureBaseCell.h"
#import "ConfigCellBaseView.h"
#import "ConfigMainTitleView.h"
#import "ConfigSubTitleView.h"
#import "ConfigCellNormalView.h"
#import "ConfigCellPhoneView.h"
#import "ConfigCellEnclosureView.h"
#import "ConfigCellTextView.h"
#import "ConfigCellAttributeView.h"
#import "ConfigSubPhotoView.h"


@interface ConfigureBaseCell ()
@property (nonatomic, strong) UIView *baseCellView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIStackView *cellStackView;

@end



@implementation ConfigureBaseCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initBaseCellUI];
    }
    return self;
}


- (void)awakeFromNib{
    [super awakeFromNib];
    [self initBaseCellUI];
}

-(void)initBaseCellUI{
    [self.contentView addSubview:self.baseCellView];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

-(void)layoutSubviews{

    [self.baseCellView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(_cellUIConfig.cellContentEdge);
    }];
        
    [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(CC_OnePixel);
    }];
}

#pragma mark getter UI
/// baseCellView
-(UIView *)baseCellView{
    if (!_baseCellView) {
        _baseCellView = [UIView new];
        _baseCellView.layer.borderColor = [UIColor clearColor].CGColor;
        _baseCellView.layer.borderWidth  = 0.0;
        _baseCellView.layer.cornerRadius = 0.0;
    }
    return _baseCellView;
}


-(UIStackView *)cellStackView{
    if (!_cellStackView) {
        _cellStackView = [UIStackView new];
        _cellStackView.axis = UILayoutConstraintAxisVertical;
        _cellStackView.distribution = UIStackViewDistributionFill;
        _cellStackView.spacing = 8;
        _cellStackView.alignment = UIStackViewAlignmentFill;
        
    }
    return _cellStackView;
}

-(UIView *)lineView{
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
    }
    return _lineView;
}

#pragma mark Setter 基本配置信息
-(void)setCellUIConfig:(CellConfigSetting *)cellUIConfig{
    if (cellUIConfig) {
        _cellUIConfig = cellUIConfig;
        [self configCellSubView];
        [self layoutIfNeeded];
    }
}

#pragma mark 配置信息处理
///根据配置布局
-(void)configCellSubView{
    [self configCellStackView];
    switch (_cellUIConfig.cellType) {
        case 1:{
            [self.cellStackView mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.left.right.bottom.top.mas_equalTo(self.baseCellView);
            }];
            [self.contentView addSubview:self.lineView];
        }break;
        case 2:{
            self.baseCellView.backgroundColor = [UIColor whiteColor];
            [self.cellStackView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_offset(UIEdgeInsetsMake(10, 10, 10, 10));
            }];
            [self defaultConfigSetting];
        }break;
        case 3:{
            [self.cellStackView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.top.mas_equalTo(self.baseCellView);
            }];
        }break;
        default: break;
    }
}

-(void)defaultConfigSetting{
    _baseCellView.layer.borderColor  = [UIColor colorWithHexString:_cellUIConfig.cellBorderColor].CGColor;
    _baseCellView.layer.borderWidth  = _cellUIConfig.cellBorderWidth;
    _baseCellView.layer.cornerRadius = _cellUIConfig.cellCornerRadius;
}

/// 根据配置信息在 cellStackView 上添加对应的SubView
-(void)configCellStackView{
    [self.cellStackView removeAllSubviews];
    [self.baseCellView addSubview:self.cellStackView];
    [_cellUIConfig.subConfigData enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CellSubViewsConfigSetting *subSetting = (CellSubViewsConfigSetting *)obj;
        
        switch (subSetting.celltype) {
            case CMainTitle:{
                ConfigMainTitleView *maintitle = [ConfigMainTitleView new];
                maintitle.leftViewWidth = _cellUIConfig.frontSpace;
                maintitle.viewConfiSets = subSetting;
                [self.cellStackView addArrangedSubview:maintitle];
            }
                break;
            case CContentType:{
                ConfigCellNormalView *normaltitle = [ConfigCellNormalView new];
                normaltitle.leftViewWidth = _cellUIConfig.frontSpace;
                normaltitle.viewConfiSets = subSetting;
                [self.cellStackView addArrangedSubview:normaltitle];
                [normaltitle.tapGestureRecognizer.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
                    [self.clickSeletorSubject sendNext:@(idx)];
                }];
            }
                break;
            case CPhoneType:{
                ConfigCellPhoneView *phoneView = [ConfigCellPhoneView new];
                phoneView.leftViewWidth = _cellUIConfig.frontSpace;
                phoneView.viewConfiSets = subSetting;
                [self.cellStackView addArrangedSubview:phoneView];
            }
                break;
            case CSubTitileType:{
                ConfigSubTitleView *subtitle = [ConfigSubTitleView new];
                subtitle.leftViewWidth = _cellUIConfig.frontSpace;
                subtitle.viewConfiSets = subSetting;
                [self.cellStackView addArrangedSubview:subtitle];
            }
                break;
            case CEnclosureType:{
                ConfigCellEnclosureView *enclourseView = [ConfigCellEnclosureView new];
                enclourseView.leftViewWidth = _cellUIConfig.frontSpace;
                enclourseView.viewConfiSets = subSetting;
                [self.cellStackView addArrangedSubview:enclourseView];
            }
                break;
            case CTextType:{
                ConfigCellTextView *cellTextView = [ConfigCellTextView new];
                cellTextView.viewConfiSets = subSetting;
                [self.cellStackView addArrangedSubview:cellTextView];
            }
                break;
            case CAttributeType:{
                ConfigCellAttributeView *cellAttriView = [ConfigCellAttributeView new];
                cellAttriView.leftViewWidth = _cellUIConfig.frontSpace;
                cellAttriView.viewConfiSets = subSetting;
                [self.cellStackView addArrangedSubview:cellAttriView];
            }
                break;
            case CPhotoType:{
                    ConfigSubPhotoView *cellPhotoView = [ConfigSubPhotoView new];
                    cellPhotoView.leftViewWidth = _cellUIConfig.frontSpace;
                    cellPhotoView.viewConfiSets = subSetting;
                    [self.cellStackView addArrangedSubview:cellPhotoView];
            }
                break;
                
            default:
                break;
        }
    }];
    
}

#pragma mark 设置值
-(void)setDataValueDict:(NSDictionary *)dataValueDict{
    if (_cellUIConfig.subConfigData.count>0) {
        [self.cellStackView.arrangedSubviews enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            CellSubViewsConfigSetting *config = _cellUIConfig.subConfigData[idx];
            NSString *key = config.cKeyValue;
            
            if ([obj isKindOfClass:[ConfigMainTitleView class]]) {
                
                ConfigMainTitleView *subView1 = (ConfigMainTitleView *)obj;
                NSString *string = [[dataValueDict valueForKey:key] description];
                [subView1 setBaseVaule:string.length>0?string:config.cDefalutValue];
                
            }else if ([obj isKindOfClass:[ConfigCellNormalView class]]){
                
                ConfigCellNormalView *subView2 = (ConfigCellNormalView *)obj;
                NSString *string = [[dataValueDict valueForKey:key] description];
                [subView2 setBaseVaule:string.length>0?string:config.cDefalutValue];
                
            }else if ([obj isKindOfClass:[ConfigCellPhoneView class]]){
                
                ConfigCellPhoneView *subView3 = (ConfigCellPhoneView *)obj;
                NSString *string = [[dataValueDict valueForKey:key] description];
                [subView3 setBaseVaule:string];
                
            }else if ([obj isKindOfClass:[ConfigCellEnclosureView class]]){
                 ConfigCellEnclosureView *subView4 = (ConfigCellEnclosureView *)obj;
                
                if ([[dataValueDict objectForKey:key] isKindOfClass:[NSArray class]]) {
                    NSArray *array = [dataValueDict objectForKey:key];
                    if (array.count>0) {
                        [subView4 setFilesDataSource:[dataValueDict objectForKey:key]];
                    }else{
                        [subView4 removeFromSuperview];
                    }
                }

            }else if([obj isKindOfClass:[ConfigCellTextView class]]){
                
                ConfigCellTextView *subView3 = (ConfigCellTextView *)obj;
                NSString *string = [[dataValueDict valueForKey:key] description];
                [subView3 setBaseVaule:string];
                
            }else if([obj isKindOfClass:[ConfigCellAttributeView class]]){
                
                ConfigCellAttributeView *subView3 = (ConfigCellAttributeView *)obj;
                NSString *string = [[dataValueDict valueForKey:key] description];
                NSString *colorKey = config.hexColor;
                NSString *hexString =[[dataValueDict valueForKey:colorKey] description];
                [subView3 setBaseVaule:string];
                [subView3 setHexColorString:hexString];
                
            }else if([obj isKindOfClass:[ConfigSubPhotoView class]]){
                
                ConfigSubPhotoView *subView3 = (ConfigSubPhotoView *)obj;
                [subView3 setPhotoArray:[dataValueDict objectForKey:config.cKeyValue]];
                
            }else{
                
            }
        }];
        
    }
}


@end

